import { ref } from "vue"
import * as PIXI from "pixi.js"
import { Graphics } from "pixi.js"
import CONFIG from '@/config'

class PIXIAppHandler{

  private static handler: PIXIAppHandler

  private pixiapp!: PIXI.Application

    private constructor(){
      this.pixiapp = new PIXI.Application({
        width: CONFIG.DEFAULT_BOARD_WIDTH, // default: 800 宽度
        height: CONFIG.DEFAULT_BOARD_HEIGHT, // default: 600 高度
        antialias: CONFIG.ANTIALIAS, // default: false 反锯齿
        transparent: false, // default: false 透明度
      })
    }

    public static getInstance(): PIXIAppHandler {
      if (this.handler == null) {
        this.handler = new PIXIAppHandler();
      }
      return PIXIAppHandler.handler;
    }

    //画标尺
    drawRuler = () => {
        for(let i = 0; i <= 1500; i += 10){
          this.drawLine_2(i, 0, i, 1000, 1, .1, 0xf9f9f9)
        }
        for(let i = 0; i <= 1500; i += 10){
          this.drawLine_2(0, i, 1500, i, 1, .1, 0xf9f9f9)
        }
        for(let i = 0; i <= 1500; i += 50){
          this.drawLine_2(i, 0, i, 1000, 1, .2, 0xeeeeee)
        }
        for(let i = 0; i <= 1500; i += 50){
          this.drawLine_2(0, i, 1500, i, 1, .2, 0xeeeeee)
        }
        for(let i = 0; i <= 1500; i += 100){
          this.drawLine_2(i, 0, i, 1000, 1, .3, 0xbbbbbb)
        }
        for(let i = 0; i <= 1500; i += 100){
          this.drawLine_2(0, i, 1500, i, 1, .3, 0xbbbbbb)
        }
      }
      //画点
      drawPoint = (startx : number = 0
                 , starty : number = 0
                 , color  : number = 0x000000) => {
        let line = new Graphics()
        line.lineStyle(1, color, 1)
        line.moveTo(0, 0)
        line.lineTo(0, 0)
        line.x = startx
        line.y = starty
        this.pixiapp.stage.addChild(line)
        this.drawRuler()
      }
  
      //画线
      drawLine = (startx : number = 0
                , starty : number = 0
                , endx   : number = 0
                , endy   : number = 0
                , width  : number = 0
                , color  : number = 0x000000) => {
        let line = new Graphics()
        line.lineStyle(width, color, 1)
        line.moveTo(startx, starty)
        line.lineTo(endx, endy)
        line.x = 0
        line.y = 0
        this.pixiapp.stage.addChild(line)
        this.drawRuler()
      }
  
      //画线（不需要更新标尺）
      drawLine_2 = (startx : number
                  , starty : number
                  , endx   : number
                  , endy   : number
                  , width  : number
                  , alpha  : number
                  , color  : number) => {
        let line = new Graphics()
        line.lineStyle(width, color, alpha)
        line.moveTo(startx, starty)
        line.lineTo(endx, endy)
        line.x = 0
        line.y = 0
        this.pixiapp.stage.addChild(line)
      }
  
      //画圆
      drawCircle = (startx : number = 0
                  , starty : number = 0
                  , radius : number = 0
                  , color  : number = 0x000000) => {
        let circle = new Graphics()
        circle.lineStyle(0, color, 1)
        circle.beginFill(color)
        circle.drawCircle(0, 0, radius)
        circle.endFill()
        circle.x = startx
        circle.y = starty
        this.pixiapp.stage.addChild(circle)
        this.drawRuler()
      }
  
      //画空心圆
      drawHollowCircle = (color  : number
                        , radius : number
                        , startx : number
                        , starty : number) => {
        let circle = new Graphics()
        circle.lineStyle(1, color, 1)
        circle.beginFill(0x000000, 0)
        circle.drawCircle(0, 0, radius)
        circle.endFill()
        circle.x = startx
        circle.y = starty
        this.pixiapp.stage.addChild(circle)
        this.drawRuler()
      }
  
      //画长方形
      drawRectangle = (startx : number
                     , starty : number
                     , endx   : number
                     , endy   : number
                     , color  : number) => {
        let rectangle = new Graphics()
        rectangle.beginFill(color)
        rectangle.drawRect(startx, starty, endx-startx, endy-starty)
        rectangle.endFill()
        rectangle.x = 0
        rectangle.y = 0
        this.pixiapp.stage.addChild(rectangle)
        this.drawRuler()
      }
  
      //画空心长方形
      drawHollowRectangle = (color  : number
                           , startx : number
                           , starty : number
                           , width  : number
                           , height : number) => {
        let rectangle = new Graphics()
        rectangle.lineStyle(1, color, 1)
        rectangle.beginFill(color, 0)
        rectangle.drawRect(startx, starty, width, height)
        rectangle.endFill()
        rectangle.x = 0
        rectangle.y = 0
        this.pixiapp.stage.addChild(rectangle)
        this.drawRuler()
      }

      none = () => {
          ;
      }
  
}
export default PIXIAppHandler